package com.datastructure.test.doubletoken.lengthofnorepeatstring.self;

import java.util.HashMap;
import java.util.Map;

public class MaxLengthOfNoRepeatingString {

    public static void main(String[] args) {
        MaxLengthOfNoRepeatingString maxLengthOfNoRepeatingString = new MaxLengthOfNoRepeatingString();
        String data = "aabfguicnavf";
        System.out.println(maxLengthOfNoRepeatingString.findMaxLengthOfNoRepeatString(data));
    }

    public String findMaxLengthOfNoRepeatString(String data){
        if(data.length()==0||data.length()==1){
            return data;
        }
        Map<Character,Integer> res = new HashMap<>();
        int maxLength = 0;
        int indexI = 0;
        for (int i = 0,j=0; i < data.length(); i++) {
            if(res.containsKey(data.charAt(i))){
                j = Math.max(j, res.get(data.charAt(i))+1);
            }
            res.put(data.charAt(i),i);
            if(i-j+1>maxLength){
                maxLength=i-j+1;
                indexI=j;
            }
        }
        return data.substring(indexI,maxLength);
    }
}
